Ext.regApplication({
    name: 'myapp',
    launch: function () {
    	//alert('regApplication'); 
        this.launched = true;
        this.mainLaunch();
    },
    mainLaunch: function () {
    	// running on android emulator
        //if (!device || !this.launched) { return; }

    	// running in chrome 
    	if (!this.launched) { return; }
    	this.isChrome = true;

        this.isOnline = false;
        if (this.isChrome)
        	this.isOnline = true;
        else
        	checkConnection();

        this.geoLocation = null;
        if (this.isOnline)
        	this.geoLocation = setupGeoLocation();
        
        // setup a timer to check for connection so if user loses and 
        // gains connection while using app, map has better chance of 
        // functioning
        window.setInterval(Ext.createDelegate(checkConnection, this), 10000); // every 10 secs

        // and Continue
    	console.log('mainLaunch!');
    	//this.views.viewport = new this.views.Viewport();
        this.views.viewport = new this.views.Viewport();
    }
});

function checkConnection() {
	console.log('checking connection');
	if (myapp.isChrome)
		return;

    var networkState = navigator.network.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.NONE]     = 'No network connection';

    if(networkState == Connection.NONE || networkState == null || networkState == Connection.UNKNOWN)
    	myapp.isOnline = false;
    	//return false;
    else
    	myapp.isOnline = true;
    	//return true;
    //alert('Connection type: ' + states[networkState]);
    //alert('myapp.isOnline: ' + myapp.isOnline);
};

function setupGeoLocation(){
	if (!myapp.isOnline)
		return;
	var geo = new Ext.util.GeoLocation({
	    autoUpdate: true,
	    listeners: {
	        locationupdate: function (geo) {
	            console.log('New latitude: ' + geo.latitude);
	        },
	        locationerror: function (   geo,
	                                    bTimeout, 
	                                    bPermissionDenied, 
	                                    bLocationUnavailable, 
	                                    message) {
	            if(bTimeout){
	                alert('Timeout occurred.');
	            }
	            else{
	                alert('Error occurred.');
	            }
	        }
	    }
	});
	geo.updateLocation();
	return geo;
 }
Ext.Viewport.override({
    scrollToTop: function() {
        if (Ext.is.iOS) {
            if (Ext.is.Phone) {   
                document.body.scrollTop = 0; 
                 //was "document.body.scrollHeight;", instead of 0 as it is now.
            }
        }
        else if (Ext.is.Blackberry) {
            window.scrollTo(0, 1000);
        }
        else {
            window.scrollTo(0, 1);
        }
    }
}); 